from flask import render_template, request, redirect
from flask.globals import session
from flask.helpers import url_for
from sqlalchemy import or_
from app import app, db
from app.models import User,Topic

jedi = "of the jedi"

@app.route('/<int:page>')
@app.route('/index/<int:page>')
def index(page):
    if page:
        entries = Topic.query.paginate(page, 9, False)
    else:
        entries = Topic.query.paginate(1, 9, False)
    next_url = url_for('index', page=entries.next_num) \
        if entries.has_next else None
    prev_url = url_for('index', page=entries.prev_num) \
        if entries.has_prev else None

    return render_template('index.html', title='Home', 
                           entries=entries, next_url=next_url,
                           prev_url=prev_url)


@app.route('/topic',methods=['GET'])
def topic():
    page = int(request.args.get('page')) if request.args.get('page') else None
    search = request.args.get('search')
    query = Topic.query.join(User,Topic.uid == User.uid).filter(or_(User.credibility == 'S',Topic.title.contains("转租"))).filter(~Topic.title.contains("求"))
    print(session.get("search"))
    if request.args.get('search'):
        search = request.args.get('search')
        session['search'] = request.args.get('search')
    else:
        search = session.get("search")
    if search and search!='""':
        query = query.filter(Topic.title.contains(search))
    query = query.order_by(Topic.created.desc())

    if page:
        entries = query.paginate(page, 9, False)
    else:
        entries = query.paginate(1, 9, False)
    next_url = url_for('topic', page=entries.next_num) \
        if entries.has_next else None
    prev_url = url_for('topic', page=entries.prev_num) \
        if entries.has_prev else None
    return render_template('index.html', title='Home', 
                           entries=entries, next_url=next_url,
                           prev_url=prev_url)


@app.route('/add', methods=['POST'])
def add():
    if request.method == 'POST':
        form = request.form
        title = form.get('title')
        description = form.get('description')
        if not title or description:
            entry = Entry(title = title, description = description)
            db.session.add(entry)
            db.session.commit()
            return redirect('/')

    return "of the jedi"

@app.route('/update/<int:id>')
def updateRoute(id):
    if not id or id != 0:
        entry = Entry.query.get(id)
        if entry:
            return render_template('update.html', entry=entry)

    return "of the jedi"

@app.route('/update', methods=['POST'])
def update():
    if not id or id != 0:
        entry = Entry.query.get(id)
        if entry:
            db.session.delete(entry)
            db.session.commit()
        return redirect('/')

    return "of the jedi"



@app.route('/delete')
def delete():
    id = request.args.get('id')
    if id:
        entry = Topic.query.get(id)
        if entry:
            db.session.delete(entry)
            db.session.commit()
        return redirect('/topic')

    return "of the jedi"
@app.route('/disable')
def disable():
    id = request.args.get('id')
    if id:
        User.query.filter(User.uid == id).update({'credibility':'D'})
        db.session.commit()
        return redirect('/topic')
    return "of the jedi"
@app.route('/turn/<int:id>')
def turn(id):
    if not id or id != 0:
        entry = Entry.query.get(id)
        if entry:
            entry.status = not entry.status
            db.session.commit()
        return redirect('/')

    return "of the jedi"

# @app.errorhandler(Exception)
# def error_page(e):
#     return "of the jedi"